package com.menker.app.common;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.sahasbhop.apngview.ApngDrawable;
import com.github.sahasbhop.apngview.ApngImageLoader;
import com.menker.app.R;
import com.menker.app.util.AppUtils;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;

import java.util.Date;

/**
 * Created by developer on 17-11-29.
 */

public class MyRefreshHeader extends RelativeLayout implements RefreshHeader {

    ImageView mImageView;
    TextView mTextView;

    public static String REFRESH_HEADER_PULLDOWN = "下拉可以刷新";
    public static String REFRESH_HEADER_REFRESHING = "正在刷新";
    public static String REFRESH_HEADER_RELEASE = "释放立即刷新";
    public static String REFRESH_HEADER_FINISH = "刷新完成";
    public static String REFRESH_HEADER_FAILED = "刷新失败";

    private SpinnerStyle mSpinnerStyle = SpinnerStyle.Translate;
    private ApngDrawable apngDrawable;

    public MyRefreshHeader(Context context) {
        super(context);
        initView(context,null);
    }

    public MyRefreshHeader(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context,attrs);
    }

    void initView(Context context,AttributeSet attrs) {

        LinearLayout layout = new LinearLayout(context);
        layout.setGravity(Gravity.CENTER_HORIZONTAL);
        layout.setOrientation(LinearLayout.VERTICAL);
        RelativeLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(CENTER_IN_PARENT);

        mImageView = new ImageView(context);
        LinearLayout.LayoutParams imageParams;
        int screenSize = AppUtils.getScreenSize(context);
        if (screenSize == AppUtils.SCREEN_HDPI_CODE) {
            imageParams = new LinearLayout.LayoutParams(150,150);
        }else if (screenSize == AppUtils.SCREEN_XDPI_CODE) {
            imageParams = new LinearLayout.LayoutParams(200,200);
        }else if (screenSize == AppUtils.SCREEN_XXDPI_CODE) {
            imageParams = new LinearLayout.LayoutParams(200,200);
        }else {
            imageParams = new LinearLayout.LayoutParams(200,200);
        }


        mTextView = new TextView(context);
        mTextView.setText(REFRESH_HEADER_PULLDOWN);
        mTextView.setTextColor(0xff7c7c7c);
        mTextView.setTextSize(15);
        LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textParams.setMargins(10,10,10,10);

        layout.addView(mTextView,textParams);
        layout.addView(mImageView,imageParams);

        addView(layout,layoutParams);

        TypedArray ta = context.obtainStyledAttributes(attrs, com.scwang.smartrefresh.layout.R.styleable.ClassicsHeader);
        mSpinnerStyle = SpinnerStyle.values()[ta.getInt(com.scwang.smartrefresh.layout.R.styleable.ClassicsHeader_srlClassicsSpinnerStyle,mSpinnerStyle.ordinal())];
        ta.recycle();

        String uri = "assets://apng/loading.png";
        ApngImageLoader.getInstance().displayImage(uri,mImageView);

    }

    @Override
    public void onPullingDown(float percent, int offset, int headerHeight, int extendHeight) {

    }

    @Override
    public void onReleasing(float percent, int offset, int headerHeight, int extendHeight) {

    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {

        return mSpinnerStyle;
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int extendHeight) {

    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int height, int extendHeight) {
        apngDrawable = ApngDrawable.getFromView(mImageView);

        if (apngDrawable != null) {
            apngDrawable.start();
        }
    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        apngDrawable = ApngDrawable.getFromView(mImageView);

        if (apngDrawable != null) {
            apngDrawable.stop();
        }

        if (success) {
            mTextView.setText(REFRESH_HEADER_FINISH);
        } else {
            mTextView.setText(REFRESH_HEADER_FAILED);
        }

        //  延迟多少毫秒后弹回
        return 500;
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {

        switch (newState) {

            case None:
                restoreRefreshLayoutBackground();
            case PullDownToRefresh:
                mTextView.setText(REFRESH_HEADER_PULLDOWN);
                break;
            case Refreshing:
                mTextView.setText(REFRESH_HEADER_REFRESHING);
                break;
            case ReleaseToRefresh:
                mTextView.setText(REFRESH_HEADER_RELEASE);
                replaceRefreshLayoutBackground(refreshLayout);
                break;
        }
    }


    private Runnable restoreRunnable;
    private void restoreRefreshLayoutBackground() {
        if (restoreRunnable != null) {
            restoreRunnable.run();
            restoreRunnable = null;
        }
    }

    private void replaceRefreshLayoutBackground(final RefreshLayout refreshLayout) {
        if (restoreRunnable == null && mSpinnerStyle == SpinnerStyle.FixedBehind) {
            restoreRunnable = new Runnable() {
                Drawable drawable = refreshLayout.getLayout().getBackground();
                @Override
                public void run() {
                    refreshLayout.getLayout().setBackgroundDrawable(drawable);
                }
            };
            refreshLayout.getLayout().setBackgroundDrawable(getBackground());
        }
    }
}
